<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">新风设备ip</label>
        <el-input
          v-model="query.ip"
          clearable
          placeholder="新风设备ip"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!-- <label class="el-form-item-label">实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px"
          placeholder="实验室"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        > -->
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <!-- <label class="el-form-item-label">进风口温度</label>
        <el-input v-model="query.temperature" clearable placeholder="进风口温度" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">进风口湿度</label>
        <el-input v-model="query.humidity" clearable placeholder="进风口湿度" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <!-- <label class="el-form-item-label">故障状态</label>
        <el-select
          v-model="query.status"
          clearable
          style="width: 185px"
          placeholder="故障状态"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option value="0" label="正常" />
          <el-option value="1" label="故障" />
        </el-select>
        <label class="el-form-item-label">缺风状态</label>
        <el-select
          v-model="query.lackWind"
          clearable
          style="width: 185px"
          placeholder="缺风状态"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option value="0" label="正常" />
          <el-option value="1" label="缺风" />
        </el-select> -->
        <!-- <label class="el-form-item-label">运行变频</label>
        <el-input v-model="query.transducer" clearable placeholder="运行变频" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">新风电动阀</label>
        <el-input v-model="query.electricallyOperatedValve" clearable placeholder="新风电动阀" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">报警信息</label>
        <el-input v-model="query.errorMessage" clearable placeholder="报警信息" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="550px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="100px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <el-form-item label="新风设备ip" prop="ip">
            <el-input v-model="form.ip" style="width: 370px" />
          </el-form-item>
          <el-form-item label="楼层信息" prop="floor">
            <el-select
              v-model="form.floor"
              style="width: 370px;"
              placeholder="请选择楼层信息"
            >
              <el-option label="一楼" :value="1" />
              <el-option label="二楼" :value="2" />
              <el-option label="三楼" :value="3" />
              <el-option label="四楼" :value="4" />
              <el-option label="五楼" :value="5" />
              <el-option label="六楼" :value="6" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="启停状态地址" prop="address">
            <el-input v-model="form.address" style="width: 370px" />
          </el-form-item>
          <el-form-item label="实验室" prop="labId">
            <el-select
              v-model="form.labId"
              style="width: 370px"
              clearable
              filterable
              :remote-method="getLabs"
              placeholder="请选择实验室"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item label="进风口温度" prop="temperature">
            <el-input v-model="form.temperature" style="width: 370px" />
          </el-form-item>
          <el-form-item label="进风口湿度" prop="humidity">
            <el-input v-model="form.humidity" style="width: 370px" />
          </el-form-item>
          <el-form-item label="运行变频" prop="transducer">
            <el-input
              v-model="form.transducer"
              style="width: 370px"
            />
          </el-form-item>
          <el-form-item label="新风电动阀" prop="electricallyOperatedValve">
            <el-input
              v-model="form.electricallyOperatedValve"
              style="width: 370px"
            />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="ip" label="新风设备ip" />
        <el-table-column prop="floor" label="楼层信息">
          <template slot-scope="scope">
            <span v-if="scope.row.floor == 1">一楼</span>
            <span v-if="scope.row.floor == 2">二楼</span>
            <span v-if="scope.row.floor == 3">三楼</span>
            <span v-if="scope.row.floor == 4">四楼</span>
            <span v-if="scope.row.floor == 5">五楼</span>
            <span v-if="scope.row.floor == 6">六楼</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="labName" label="实验室" /> -->
        <!-- <el-table-column prop="temperature" label="进风口温度">
          <template slot-scope="scope">
            <div v-if="scope.row.temperature">{{ scope.row.temperature }}°C</div>
            <div v-if="scope.row.temperature == null">- -</div>
            < {{ scope.row.temperature }}°C >
          <! </template>
        </el-table-column> -->
        <!-- <el-table-column prop="humidity" label="进风口湿度">
          <template slot-scope="scope">
            <div v-if="scope.row.humidity">{{ scope.row.humidity }}%</div>
            <div v-if="scope.row.humidity == null">- -</div>
            {{ scope.row.humidity }}% 
          </template>
        </el-table-column>
        <el-table-column prop="transducer" label="运行变频">
          <template slot-scope="scope">
            <div v-if="scope.row.transducer">{{ scope.row.transducer }}m/s</div>
            <div v-if="scope.row.transducer == null">- -</div>
            {{ scope.row.transducer }}m/s
          </template>
        </el-table-column>
        <el-table-column prop="electricallyOperatedValve" label="新风电动阀">
          <template slot-scope="scope">
            <div v-if="scope.row.electricallyOperatedValve">
              {{ scope.row.electricallyOperatedValve }}%
            </div>
            <div v-if="scope.row.electricallyOperatedValve == null">- -</div>
            {{ scope.row.electricallyOperatedValve }}%
          </template>
        </el-table-column>
        <el-table-column prop="status" label="故障状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0">正常</span>
            <span v-if="scope.row.status == 1">故障</span>
          </template>
        </el-table-column>
        <el-table-column prop="lackWind" label="缺风状态">
          <template slot-scope="scope">
            <span v-if="scope.row.lackWind == 0">正常</span>
            <span v-if="scope.row.lackWind == 1">缺风</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="indoorTemperature" label="室内温度">
          <template slot-scope="scope">
            {{ scope.row.indoorTemperature }}°C
          </template>
        </el-table-column>
        <el-table-column prop="ambientTemperature" label="环境温度">
          <template slot-scope="scope">
            {{ scope.row.ambientTemperature }}°C
          </template>
        </el-table-column>
        <!-- <el-table-column prop="address" label="启停状态地址" /> -->
        <!-- <el-table-column prop="errorMessage" label="报警信息" /> -->
        <el-table-column prop="isOpen" label="启停状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.isOpen == 1">开启</el-tag>
            <el-tag type="danger" v-if="scope.row.isOpen == 0">停止</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          v-if="
            checkPer([
              'admin',
              'freshAirVentilator:edit',
              'freshAirVentilator:del',
            ])
          "
          label="操作"
          width="250px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
            <el-button
              size="mini"
              type="success"
              @click="toOpenDoor(scope.row.id)"
              >开启</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="toCloseDoor(scope.row.id)"
              >停止</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudFreshAirVentilator from "@/api/freshAirVentilator";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { get } from "@/api/lab";
import { sendMsgToClient } from "@/api/freshAirVentilator";

const defaultForm = {
  id: null,
  name: null,
  ip: null,
  labId: null,
  temperature: null,
  humidity: null,
  status: null,
  lackWind: null,
  transducer: null,
  electricallyOperatedValve: null,
  errorMessage: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
};
export default {
  name: "FreshAirVentilator",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "新风设备",
      url: "api/freshAirVentilator",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudFreshAirVentilator },
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "freshAirVentilator:add"],
        edit: ["admin", "freshAirVentilator:edit"],
        del: ["admin", "freshAirVentilator:del"],
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        ip: [
          { required: true, message: "新风设备ip不能为空", trigger: "blur" },
        ],
        labId: [{ required: true, message: "实验室不能为空", trigger: "blur" }],
        temperature: [
          { required: true, message: "进风口温度不能为空", trigger: "blur" },
        ],
        humidity: [
          { required: true, message: "进风口湿度不能为空", trigger: "blur" },
        ],
        transducer: [
          { required: true, message: "运行变频不能为空", trigger: "blur" },
        ],
        electricallyOperatedValve: [
          { required: true, message: "新风电动阀不能为空", trigger: "blur" },
        ],
        address: [
          { required: true, message: "启停状态地址不能为空", trigger: "blur" },
        ],
        floor: [
          { required: true, message: "楼层信息不能为空", trigger: "blur" }
        ]
      },
      queryTypeOptions: [
        { key: "name", display_name: "名称" },
        { key: "ip", display_name: "新风设备ip" },
        { key: "labId", display_name: "实验室" },
        { key: "temperature", display_name: "进风口温度" },
        { key: "humidity", display_name: "进风口湿度" },
        { key: "status", display_name: "故障状态" },
        { key: "lackWind", display_name: "缺风状态" },
        { key: "transducer", display_name: "运行变频" },
        { key: "electricallyOperatedValve", display_name: "新风电动阀" },
        { key: "errorMessage", display_name: "报警信息" },
      ],
      labs: [],
    };
  },
  created() {
    this.getLabs();
  },
  methods: {
    getLabs() {
      get()
        .then((response) => {
          this.labs = response;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    toOpenDoor(id) {
      const loading = this.$loading({
        lock: true,
        text: "远程连接失败，请刷新页面重试...",
        spinner: "el-icon-loading",
        background: "rgba(255,255,255,0.8)",
      });
      sendMsgToClient({
        id,
        isOpen: 1,
      }).then((response) => {
        loading.close();
        this.$message.success("已开启");
        this.crud.refresh();
      });
    },
    toCloseDoor(id) {
      const loading = this.$loading({
        lock: true,
        text: "远程连接失败，请刷新页面重试...",
        spinner: "el-icon-loading",
        background: "rgba(255,255,255,0.8)",
      });
      sendMsgToClient({
        id,
        isOpen: 0,
      }).then((response) => {
        loading.close();
        this.$message.warning("已停止");
        this.crud.refresh();
      });
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
  },
};
</script>

<style scoped lang="scss">
.el-table ::v-deep td.el-table__cell div {
  display: flex;
  align-items: center;

  .el-button {
    width: 43px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    margin-left: 5px !important;
    padding: 0;
  }
}
</style>
